package com.team.arms.common;

import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;

/**
 * @Author ZZF
 * @Time 2021/07/27 12:05
 * 通用返回格式
 */
@Data
@AllArgsConstructor
public class Result<T> {
    /**
     * 响应码
     */
    @ApiModelProperty(value = "响应码", name = "code", required = true)
    private int code;
    /**
     * 响应消息
     */
    @ApiModelProperty(value = "响应消息", name = "message", required = true)
    private Object message;
    /**
     * 返回结果，通常是 Bean 对象对应的 JSON 数据
     */
    @ApiModelProperty(value = "返回结果", name = "data", required = true)
    private T data;

    public Result(){

    }

    /**
     * 不需要返回data时使用
     * @param code
     * @param message
     */
    public Result(int code, String message) {
        this.code = code;
        this.message = message;
        this.data = null;
    }

    public Result(ResultEnum resultEnum, T data) {
        this.code = resultEnum.getCode();
        this.message = resultEnum.getMsg();
        this.data = data;
    }

    public Result(ResultEnum resultEnum) {
        this(resultEnum, null);
    }

    public static Result success(String message){
        return new Result(200,message,null);
    }

    public static Result success(Object message){
        return new Result(200,message,null);
    }

    public static Result error(String message){
        return new Result(600,message,null);
    }

    public Result data(T data) {
        this.data = data;
        return this;
    }
}
